package com.yihuan.controller;

import com.yihuan.entity.CollectionRecycleEntity;
import com.yihuan.global.MessageConstant;
import com.yihuan.service.CollectionRecycleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:梦醒
 * Date: 2022/5/5 15:25
 * Dream:
 * 信言不美，美言不信。
 * 善者不辩，辩者不善。
 * 知者不博，博者不知。
 * Function:
 */
@RestController
public class CollectionRecycleController {
    int pageNumber = 0;
    int count = 0;
    //使用map存储数据
    private Map<String, Object> map = new HashMap<>();
    //数据页数和起始值
    private int num;
    @Autowired
    CollectionRecycleService collectionRecycleService;
    
    /**
     * 注册废品收藏信息
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/insertCollectionRecycleByCollectionRecycleData")
    public Map<String, Object> insertCollectionRecycleByCollectionRecycleData(@RequestBody CollectionRecycleEntity model, HttpSession session) {
        //设置用户编号
        model.setUserID((Integer) session.getAttribute("UserID_4"));
        //设置时间的格式
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取时间的方法
        Date d = new Date();
        //将获取的时间转换成设置的时间格式进行存储
        //登记时间
        model.setCollectionRecycleDate(sf.format(d));
        //设置收藏状态
        model.setCollectionRecycleState(1);
        //注册废品收藏信息
        int insertCollectionRecycleByCollectionRecycleData = collectionRecycleService.insertCollectionRecycleByCollectionRecycleData(model);
        //用户医院
        if (insertCollectionRecycleByCollectionRecycleData == 1) {
            map.put("code", MessageConstant.SUCCESS_CODE);
            map.put("explain", MessageConstant.COLLECTION_RECYCLE_INSERT_SUCCESS);
            return map;
        }
        map.put("code", MessageConstant.FALL_CODE);
        map.put("explain", MessageConstant.COLLECTION_RECYCLE_INSERT_FALL);
        return map;
    }
    
    /**
     * 查询公司收藏信息是否已存在过
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findCollectionRecycleByCollectionRecycleByID")
    public Map<String, Object> findCollectionRecycleByCollectionRecycleByID(@RequestBody CollectionRecycleEntity model, HttpSession session) {
        //设置用户编号
        model.setUserID((Integer) session.getAttribute("UserID_4"));
        //新增公司收藏信息
        List<CollectionRecycleEntity> findCollectionRecycleByCollectionRecycleByID = collectionRecycleService.findCollectionRecycleByCollectionRecycleByID(model);
        
        //用户医院
        if (findCollectionRecycleByCollectionRecycleByID.size() == 1) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain", MessageConstant.COLLECTION_RECYCLE_SELECT_ID_SUCCESS);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("explain", MessageConstant.COLLECTION_RECYCLE_SELECT_ID_FALL);
        return map;
    }
    
    /**
     * 取消收藏
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/updateCollectionRecycleByCollectionRecycle")
    public Map<String, Object> updateCollectionRecycleByCollectionRecycle(@RequestBody CollectionRecycleEntity model, HttpSession session) {
        //设置收藏状态
        model.setCollectionRecycleState(2);
        //新增公司收藏信息
        int updateCollectionRecycleByCollectionRecycle = collectionRecycleService.updateCollectionRecycleByCollectionRecycle(model);
        //修改收藏
        if (updateCollectionRecycleByCollectionRecycle == 1) {
            map.put("code", MessageConstant.SUCCESS_CODE);
            map.put("explain", MessageConstant.COLLECTION_RECYCLE_UPDATE_SUCCESS);
            return map;
        }
        map.put("code", MessageConstant.FALL_CODE);
        map.put("explain", MessageConstant.COLLECTION_RECYCLE_UPDATE_FALL);
        return map;
    }
    
    /**
     * 查询用户收藏
     *
     * @param session
     * @return
     */
    @RequestMapping("/findCollectionRecycleByCollectionRecycle")
    public Map<String, Object> findCollectionRecycleByCollectionRecycle(HttpSession session) {
        CollectionRecycleEntity model = new CollectionRecycleEntity();
        //设置用户编号
        model.setUserID((Integer) session.getAttribute("UserID_4"));
        //新增公司收藏信息
        List<CollectionRecycleEntity> findCollectionRecycleByCollectionRecycle = collectionRecycleService.findCollectionRecycleByCollectionRecycle(model);
        //查询用户收藏
        if (findCollectionRecycleByCollectionRecycle.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("collection_list", findCollectionRecycleByCollectionRecycle);
            map.put("explain", MessageConstant.COLLECTION_RECYCLE_SELECT_FALL);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("collection_list", findCollectionRecycleByCollectionRecycle);
        map.put("explain", MessageConstant.COLLECTION_RECYCLE_SELECT_SUCCESS);
        return map;
    }
    
}
